const {exec} = require('child_process');
const fs = require('node:fs');

async function getDeviceList() {
    const command = 'chcp 65001 && powershell -Command "Get-PnpDevice | ConvertTo-Json -Compress"';

    return new Promise((resolve, reject) => {
        exec(command, {maxBuffer: 1024 * 1000 * 10}, (error: any, stdout: string, stderr: string) => {
            if (error) {
                console.error(`getDeviceList 错误: ${error.message}`);
                return null;
            }
            if (stderr) {
                console.error(`stderr: ${stderr}`);
                return null;
            }

            const stringLength = stdout.length;
            const driversStr = stdout.substring(25, stringLength);
            const drivers = JSON.parse(driversStr);
            resolve(drivers);
        });
    });
}


export default getDeviceList;